package com.example.ywr.repository;

import com.example.ywr.entity.Product;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 商品数据访问层
 */
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {

    /**
     * 根据商品名称模糊查询
     */
    Page<Product> findByNameContainingAndIsDeletedFalse(String name, Pageable pageable);

    /**
     * 根据分类ID查询商品
     */
    Page<Product> findByCategoryIdAndIsDeletedFalse(Long categoryId, Pageable pageable);

    /**
     * 根据状态查询商品
     */
    Page<Product> findByStatusAndIsDeletedFalse(Integer status, Pageable pageable);

    /**
     * 根据分类ID和状态查询商品
     */
    Page<Product> findByCategoryIdAndStatusAndIsDeletedFalse(Long categoryId, Integer status, Pageable pageable);

    /**
     * 更新商品状态
     */
    @Modifying
    @Query("UPDATE Product p SET p.status = ?2 WHERE p.id = ?1")
    int updateStatus(Long id, Integer status);

    /**
     * 批量更新商品状态
     */
    @Modifying
    @Query("UPDATE Product p SET p.status = ?2 WHERE p.id IN ?1")
    int batchUpdateStatus(List<Long> ids, Integer status);
}